package com.wanmait.zaisousuo.service.Impl;

import com.github.pagehelper.PageInfo;
import com.wanmait.zaisousuo.mapper.GarbageMapper;
import com.wanmait.zaisousuo.mapper.GarbageTypeCityMapper;
import com.wanmait.zaisousuo.pojo.GarbageTypeCity;
import com.wanmait.zaisousuo.service.GarbageTypeService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import com.wanmait.zaisousuo.mapper.GarbageTypeMapper;
import com.wanmait.zaisousuo.pojo.GarbageType;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class GarbageTypeServiceImpl implements GarbageTypeService {

    @Resource
    private GarbageTypeMapper garbageTypeMapper;
    @Resource
    private GarbageTypeCityMapper garbageTypeCityMapper;

    @Override
    public void insert(GarbageType garbageType) {
        garbageTypeMapper.insert(garbageType);
    }

    @Override
    public void update(GarbageType object) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public GarbageType findById(Integer id) {
        return  garbageTypeMapper.selectByPrimaryKey(id);
    }


    //查询出所有垃圾类型
    @Cacheable(value = "TypeCache",key = "'findAllGarbageType'")
    @Override
    public List<GarbageType> findAll() {
        return garbageTypeMapper.findAll();
    }


    //根据城市id查询
    @Cacheable(value = "TypeCache",key = "'findAllTypeByCityId'+#id")
    @Override
    public List<GarbageType> findAllByCityId(Integer id){
        List<GarbageTypeCity> garbageTypeCities=garbageTypeCityMapper.findAllTypeByCityId(id);
        List<GarbageType> garbageTypes=new ArrayList<>();
        GarbageType garbageType=null;
        for(int i=0;i<garbageTypeCities.size();i++){
           garbageType=new GarbageType();
           garbageType=garbageTypeCities.get(i).getGarbageType();
           garbageTypes.add(garbageType);
        }
        if(garbageTypes==null||garbageTypes.size()<4){
            garbageTypes=this.findAll();
            return garbageTypes;
        }
        else{
            return garbageTypes;
        }

    }

    //找到所有的分类，包括默认的
    @Override
    public List<GarbageType> findAllType() {
        return garbageTypeMapper.findAllType();
    }


    @Override
    public List<GarbageType> findAll(GarbageType object) {
        return null;
    }

    @Override
    public int getDataCount() {
        return 0;
    }

    @Override
    public int getDataCount(GarbageType object) {
        return 0;
    }

    @Override
    public PageInfo<GarbageType> findList(int pageNum) {
        return null;
    }

    //更改分类的状态
    @Override
    public Boolean changeTypeState(Boolean flag, Integer id) {
        garbageTypeMapper.changeTypeState(flag,id);
        return null;
    }

    //后台用查询所有的垃圾类型
    @Cacheable(value = "TypeCache",key = "'findAll2GarbageType'")
    @Override
    public  List<GarbageType>  findAll2() {
        return garbageTypeMapper.findAll2();
    }

    //清除类型缓存
    @CacheEvict(value = "TypeCache",allEntries = true)
    @Override
    public void clearCache() {}
}
